home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 06 Barnes, Hutchens / Listing1.cpp
C/C++ Source or Header  |  2001-12-09  |  1KB  |  43 lines

  1. /* Copyright (C) Jonty Barnes and Jason Hutchens, 2001. 
  2.  * All rights reserved worldwide.
  3.  *
  4.  * This software is provided "as is" without express or implied
  5.  * warranties. You may freely copy and compile this source into
  6.  * applications you distribute provided that the copyright text
  7.  * below is included in the resulting source code, for example:
  8.  * "Portions Copyright (C) Jonty Barnes and Jason Hutchens, 2001"
  9.  */
  10.  
  11. // C source of a character-level 1st-order Markov model.
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. int main(int argc,char** argv) {
  17.    int C[258][258],context,thresh,c=256,i,j;
  18.    FILE* file;
  19.    for(i=0; i<258; ++i)
  20.       for(j=0; j<258; ++j)
  21.          C[i][j]=0;
  22.    if(argc!=2) return 1;
  23.    file=fopen(argv[1],"r");
  24.    if(!file) return 2;
  25.    while(!feof(file)) {
  26.       context=c; c=fgetc(file);
  27.       if(c==-1) c=256;
  28.       ++C[context][c]; ++C[context][257];
  29.    }
  30.    fclose(file);
  31.    srand(time(NULL));
  32.    do {
  33.       context=c; c=-1;
  34.       thresh=rand()%C[context][257];
  35.       do {
  36.          thresh-=C[context][++c];
  37. } while(thresh>=0);
  38.       fprintf(stdout,"%c",(char)c);
  39.    } while(c!=256);
  40.    return 0;
  41. }
  42.  
  43.